package com.win.oauth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fyl.controller.BaseController;
import com.fyl.core.advance.constant.enums.CommonResponseEnum;
import com.fyl.core.response.RS;
import com.fyl.utils.PageQueryWrappers;
import com.win.oauth.entity.Dict;
import com.win.oauth.service.IDictService;
import com.win.oauth.utils.ExportUtil;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fyl
 * @since 2020-09-03
 */
@RestController
@RequestMapping("/oauth/dict")
public class DictController extends BaseController<IDictService, Dict> {


    @PostMapping({"/add"})
    public RS<Void> add(@RequestBody Dict entity) {
        return this.baseService.save(entity) ? RS.genSuccess() : RS.genFail(CommonResponseEnum.FAIL);
    }

    @PostMapping({"/update"})
    public RS<Void> update(@RequestBody Dict entity) {
        return this.baseService.updateById(entity) ? RS.genSuccess() : RS.genFail(CommonResponseEnum.FAIL);
    }

    @PostMapping({"/delete"})
    public RS<Void> delete(@RequestBody Collection<Long> idList) {
        return this.baseService.removeByIds(idList) ? RS.genSuccess() : RS.genFail(CommonResponseEnum.FAIL);
    }


    @SuppressWarnings("unchecked")
    @GetMapping("/getDetail")
    public RS<List<Dict>> getDetail(@RequestParam String clazzField) {
        String[] split = clazzField.split("\\.");
        List<Dict> list = baseService.lambdaQuery().eq(Dict::getClazz, split[0]).eq(Dict::getField, split[1])
                .orderByAsc(Dict::getSeq, Dict::getId).list();
        return RS.genSuccess(list);
    }

    @GetMapping({"/export"})
    public void page(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        QueryWrapper<Dict> qw = PageQueryWrappers.pageQuery(params);
        List<Dict> list = this.baseService.list(qw);

        List<Map<String, Object>> data = list.stream().map(el -> {
            Map<String, Object> map = new HashMap<>();
            map.put("clazz", el.getClazz());
            map.put("field", el.getField());
            map.put("code", el.getCode());
            map.put("label", el.getLabel());
            map.put("remark", el.getRemark());
            return map;
        }).collect(toList());

        LinkedHashMap<String, String> header = new LinkedHashMap<>();
        header.put("类名", "clazz");
        header.put("字段", "field");
        header.put("CODE", "code");
        header.put("显示", "label");
        header.put("备注", "remark");

        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
            response.setHeader("Content-Disposition", "attachment; filename=\"dict.csv\";");
            ExportUtil.doExport(data, header, response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}